import { Button, Result, Spin } from 'antd';
import React from 'react';
import styles from './index.less';
import { useModel } from '@@/plugin-model/useModel';
import { useAccess } from 'umi';
import { history } from '@@/core/history';

export type CheckModuleProps = {
  code: string;
};
const CheckModule: React.FC<CheckModuleProps> = ({ code, children }) => {
  const loading = (
    <span className={`${styles.action} ${styles.account}`}>
      <Spin
        size="small"
        style={{
          marginLeft: 8,
          marginRight: 8,
        }}
      />
    </span>
  );
  const { initialState } = useModel('@@initialState');
  if (!initialState) {
    return loading;
  }
  const access = useAccess();
  if (access.checkExpired(code)) {
    const moduleData = access.moduleData(code);
    return (
      <Result
        status="info"
        title="功能已过期,请续费后再进行操作"
        subTitle={'过期时间:' + moduleData.endTime}
        extra={
          <Button
            type="primary"
            key="console"
            onClick={() => {
              history.push('/account/open');
            }}
          >
            去续费
          </Button>
        }
      />
    );
  }

  return <>{children}</>;
};

export default CheckModule;
